﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TasksManagerService.cs" company="JaguarLabs">
//   JaguarLabs 2013
// </copyright>
// <summary>
//   Used to provide the service.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using TasksManagerService.Providers;

namespace TasksManagerService
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using TasksManagerServices.Value_Objects.Services;
    using TasksManagerServices.ValueObjects;
    using System.ServiceModel.Activation;

    /// <summary>
    /// Used to provide the service.
    /// </summary>
    [AspNetCompatibilityRequirements(RequirementsMode
        = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TasksManagerService : ITasksManagerService
    {
        /// <summary>
        /// Gets the user tasks.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>        
        public IEnumerable<Task> GetUserTasks(string user)
        {
            return TaskProvider.GetUserTasks(user);
        }

        /// <summary>
        /// Gets the user tasks.
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public Task GetTaskDetails(string taskId)
        {
            return TaskProvider.GetTaskDetails(int.Parse(taskId));
        }

        /// <summary>
        /// Inserts the task.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="endDateStr">The end date STR.</param>
        /// <param name="priorityId">The priority id.</param>
        /// <param name="notificationDateStr">The notification date STR.</param>
        /// <param name="projectId">The project id.</param>
        /// <param name="progress">The progress.</param>
        /// <param name="showInCalendar">if set to <c>true</c> [show in calendar].</param>
        /// <param name="users">The users.</param>
        /// <returns></returns>
        public InsertResponse InsertTask(string name, string description, string endDateStr, string priorityId,
                              string notificationDateStr, string projectId, string progress, string showInCalendar,
                              string users)
        {
            try
            {
                if (string.IsNullOrEmpty(users))
                {
                    return new InsertResponse
                    {
                        ServiceResponseStatusType = ServiceResponseStatusType.Fault,
                        ErrorMessage = "Error: parameter users should not be null."
                    };
                }
                var notificationDate = DateTime.Parse(notificationDateStr.Replace("_", ":"));
                var endDate = DateTime.Parse(notificationDateStr.Replace("_", ":"));
                var usersList = users.Split(';').ToList();

                var newTaskId = TaskProvider.InsertTask(new Task
                {
                    Name = name,
                    Description = description,
                    EndDate = endDate,
                    PriorityId = int.Parse(priorityId),
                    NotificationDate = notificationDate,
                    ProjectId = int.Parse(projectId),
                    ShowInCalendar = bool.Parse(showInCalendar),
                    Progress = int.Parse(progress),
                    Users = usersList,
                    Attachments = new List<Attachment>()
                });

                return new InsertResponse
                {
                    Id = newTaskId,
                    ServiceResponseStatusType = ServiceResponseStatusType.Success
                };
            }
            catch (Exception ex)
            {
                return new InsertResponse
                {
                    ServiceResponseStatusType = ServiceResponseStatusType.Fault,
                    ErrorMessage = ex.Message
                };
            }
        }

        /// <summary>
        /// Updates the task.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="endDateStr">The end date STR.</param>
        /// <param name="priorityId">The priority id.</param>
        /// <param name="notificationDateStr">The notification date STR.</param>
        /// <param name="projectId">The project id.</param>
        /// <param name="progress">The progress.</param>
        /// <param name="showInCalendar">if set to <c>true</c> [show in calendar].</param>
        /// <param name="users">The users.</param>
        /// <returns></returns>
        public UpdateResponse UpdateTask(string id, string name, string description, string endDateStr, string priorityId,
                              string notificationDateStr, string projectId, string progress, string showInCalendar,
                              string users)
        {
            try
            {
                if (string.IsNullOrEmpty(users))
                {
                    return new UpdateResponse
                    {
                        ServiceResponseStatusType = ServiceResponseStatusType.Fault,
                        ErrorMessage = "Error: parameter users should not be null."
                    };
                }

                var notificationDate = DateTime.Parse(notificationDateStr.Replace("_", ":"));
                var endDate = DateTime.Parse(notificationDateStr.Replace("_", ":"));
                var usersList = users.Split(';').ToList();

                var updated = TaskProvider.UpdateTask(new Task
                {
                    Id = int.Parse(id),
                    Name = name,
                    Description = description,
                    EndDate = notificationDate,
                    PriorityId = int.Parse(priorityId),
                    NotificationDate = notificationDate,
                    ProjectId = int.Parse(projectId),
                    ShowInCalendar = bool.Parse(showInCalendar),
                    Progress = int.Parse(progress),
                    Users = usersList,
                    Attachments = new List<Attachment>()
                });

                return new UpdateResponse
                {
                    Updated = updated,
                    ServiceResponseStatusType = ServiceResponseStatusType.Success
                };
            }
            catch (Exception ex)
            {
                return new UpdateResponse
                {
                    ServiceResponseStatusType = ServiceResponseStatusType.Fault,
                    ErrorMessage = ex.Message
                };
            }
        }

        /// <summary>
        /// Deletes the task.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public UpdateResponse DeleteTask(string id)
        {
            try
            {
                var updated = TaskProvider.DeleteTask(int.Parse(id));
                return new UpdateResponse
                {
                    Updated = updated,
                    ServiceResponseStatusType = ServiceResponseStatusType.Success
                };
            }
            catch (Exception ex)
            {
                return new UpdateResponse
                {
                    ServiceResponseStatusType = ServiceResponseStatusType.Fault,
                    ErrorMessage = ex.Message
                };
            }
        }

        /// <summary>
        /// Gets the projects.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Project> GetProjects()
        {
            return TaskProvider.GetProjects();
        }

        /// <summary>
        /// Gets the priorities.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Priority> GetPriorities()
        {
            return TaskProvider.GetPriorities();
        }

        /// <summary>
        /// Gets the projects.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<User> GetUsers()
        {
            return TaskProvider.GetUsers();
        }

        /// <summary>
        /// Creates the project.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public InsertResponse CreateProject(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return new InsertResponse
                {
                    ServiceResponseStatusType = ServiceResponseStatusType.Fault,
                    ErrorMessage = "Error: parameter name should not be null."
                };
            }

            return new InsertResponse
                {
                    Id = TaskProvider.InsertProject(name),
                    ServiceResponseStatusType = ServiceResponseStatusType.Success
                };
        }
    }
}